route.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { listDays } from "@/lib/storage";
  2. import { getSession } from "@/lib/auth/session";
  3. import { canAccessBranch } from "@/lib/auth/permissions";
  4. import {
  5. withErrorHandling,
  6. json,
  7. badRequest,
  8. unauthorized,
  9. forbidden,
  10. } from "@/lib/api/errors";
  11. import { mapStorageReadError } from "@/lib/api/storageErrors";
  12. /**
  13. * GET /api/branches/[branch]/[year]/[month]/days
  14. *
  15. * Happy-path response must remain unchanged:
  16. * { "branch":"NL01", "year":"2024", "month":"10", "days":["23", ...] }
  17. */
  18. export const GET = withErrorHandling(
  19. async function GET(request, ctx) {
  20. const session = await getSession();
  21. if (!session) {
  22. throw unauthorized("AUTH_UNAUTHENTICATED", "Unauthorized");
  23. }
  24. const { branch, year, month } = await ctx.params;
  25. const missing = [];
  26. if (!branch) missing.push("branch");
  27. if (!year) missing.push("year");
  28. if (!month) missing.push("month");
  29. if (missing.length > 0) {
  30. throw badRequest(
  31. "VALIDATION_MISSING_PARAM",
  32. "Missing required route parameter(s)",
  33. { params: missing }
  34. );
  35. }
  36. if (!canAccessBranch(session, branch)) {
  37. throw forbidden("AUTH_FORBIDDEN_BRANCH", "Forbidden");
  38. }
  39. try {
  40. const days = await listDays(branch, year, month);
  41. return json({ branch, year, month, days }, 200);
  42. } catch (err) {
  43. throw await mapStorageReadError(err, {
  44. details: { branch, year, month },
  45. });
  46. }
  47. },
  48. { logPrefix: "[api/branches/[branch]/[year]/[month]/days]" }
  49. );